package com.bootdo.hotel.controller;

import java.text.ParseException;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.common.excel.DachuExcelUtils;
import com.bootdo.common.utils.*;
import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.haojiangboutils.ObjectCopy;
import com.bootdo.haojiangboutils.RequestParCheckUtils;
import com.bootdo.hotel.domain.*;
import com.bootdo.hotel.service.*;
import com.bootdo.lvyou.domain.LvyouScenicSpot;
import com.bootdo.lvyou.service.LvyouScenicSpotService;
import com.bootdo.redis.RedisUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;

/**
 * 酒店订单
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-10-19 08:50:34
 */
 
@Controller
@RequestMapping("/hotel/hotelOrder")
public class HotelOrderController extends BaseController{

	@Autowired
	private HotelRoomTypeService hotelRoomTypeService;
	@Autowired
	private HotelRoomFloorService hotelRoomFloorService;
	@Autowired
	private HotelRoomService hotelRoomService;
	@Autowired
	private LvyouScenicSpotService lvyouScenicSpotService;
	@Autowired
	private HotelRoomDayStatusService hotelRoomDayStatusService;
	@Autowired
	private HotelOrderService hotelOrderService;
	@Autowired
	private HotelEveryDayMoneyService hotelEveryDayMoneyService;
	@Autowired
	private HotelRoomMoneyCodeService hotelRoomMoneyCodeService;
	@Autowired
	private RedisUtil redisUtil;

	@ModelAttribute
	public HotelOrder get(@RequestParam(required=false) Long id) {
		if (id != null){
			return hotelOrderService.get(id);
		}else{
			return new HotelOrder();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/hotel/hotelOrder','hotel:hotelOrder:hotelOrder',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','hotel:hotelOrder:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("hotel:hotelOrder:hotelOrder")	
	String HotelOrder(HotelOrder hotelOrder,HttpServletRequest request,HttpServletResponse response){
	    return "hotel/hotelOrder/hotelOrder";
	}



	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("hotel:hotelOrder:hotelOrder")
	public PageUtils list(@RequestParam Map<String, Object> params,HttpServletRequest request){
		if(!isAdmin()){
			params.put("userId",getUserId());
		}
		//查询列表数据
        Query query = new Query(params);
		List<HotelOrder> hotelOrderList = hotelOrderService.list(query);
		int total = hotelOrderService.count(query);
		PageUtils pageUtils = new PageUtils(hotelOrderList, total);
		String sso_token =  request.getHeader("sso_token");
		redisUtil.set(sso_token+"query",params,60*60*24);
		return pageUtils;
	}


	@ResponseBody
	@PostMapping("/listByEndTime")
	@RequiresPermissions("hotel:hotelOrder:hotelOrder")
	public PageUtils listByEndTime(@RequestParam Map<String, Object> params,HttpServletRequest request){
		if(!isAdmin()){
			params.put("userId",getUserId());
		}
		//查询列表数据
		Query query = new Query(params);
		query.put("outDate", DateUtils.format(new Date()));
		List<HotelOrder> hotelOrderList = hotelOrderService.list(query);
		int total = hotelOrderService.count(query);
		PageUtils pageUtils = new PageUtils(hotelOrderList, total);
		return pageUtils;
	}


	@GetMapping("/add")
	@RequiresPermissions("hotel:hotelOrder:edit")
	String add(HotelOrder hotelOrder,HttpServletRequest request,HttpServletResponse response){
	    return "hotel/hotelOrder/hotelOrderedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(HotelOrder hotelOrder) {
		return R.ok().put("i",hotelOrder);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("hotel:hotelOrder:edit")
	public R save( HotelOrder hotelOrder){
	
		if(hotelOrderService.save(hotelOrder)>0){
			return R.ok();
		}
		return R.error();
	}


	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/outRoom")
	@RequiresPermissions("hotel:hotelOrder:edit")
	public R outRoom( @RequestParam("id") Long id ,HttpServletRequest request){
		if(null == id || id <= 0){
			return R.error("无效");
		}
		//退房
		HotelOrder hotelOrder = 	hotelOrderService.get(id);
		if(null == hotelOrder){
			return R.error("无效");
		}
		String nowRoomId =  request.getParameter("nowRoomId");
		if(StringUtils.isNotEmpty(nowRoomId)){

			//目前多房预订只能通过微信 所以一个if就够了
            if(hotelOrder.getOrderSrc().equals("WX")){
			    if(hotelOrder.getRoomNumber() > 1){
                    hotelOrder.setRoomNumber(hotelOrder.getRoomNumber() - 1);
                    hotelOrder.setRoomId(Long.parseLong(nowRoomId));
                    hotelOrder.setFlag("DUO");
                    hotelOrder.setStatus(0 - hotelOrder.getStatus());
                }else{
                    hotelOrder.setRoomNumber(hotelOrder.getRoomNumber() - 1);
                    hotelOrder.setRoomId(Long.parseLong(nowRoomId));
                    hotelOrder.setFlag("DUO");
                    hotelOrder.setStatus(2);
                }
			}else if(hotelOrder.getOrderSrc().equals("HOTEL")){
				if(hotelOrder.getRoomNumber() > 1){
					hotelOrder.setRoomNumber(hotelOrder.getRoomNumber() - 1);
					hotelOrder.setRoomId(Long.parseLong(nowRoomId));
					hotelOrder.setFlag("DUO");
					hotelOrder.setStatus(0 - hotelOrder.getStatus());
				}else{
					hotelOrder.setRoomNumber(hotelOrder.getRoomNumber() - 1);
					hotelOrder.setRoomId(Long.parseLong(nowRoomId));
					hotelOrder.setStatus(2);
				}
			}
		}else{
			hotelOrder.setStatus(2);
		}
		try {
			hotelOrderService.saveOrderAndDayStatus(hotelOrder);
			return R.ok("退房成功");
		}catch (Exception e){
			e.printStackTrace();
			return R.error(e.getMessage());
		}
	}


	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/jiesuanOrder")
	@RequiresPermissions("hotel:hotelOrder:edit")
	public R jiesuanOrder( @RequestParam("id") Long id,@RequestParam("money") Double money ,HttpServletRequest request){
		if(null == id || id <= 0){
			return R.error("无效");
		}
		//退房
		HotelOrder hotelOrder = 	hotelOrderService.get(id);
		if(null == hotelOrder){
			return R.error("无效");
		}
		hotelOrder.setJiesuanMoney(money);
		hotelOrder.setJiesuanTime(new Date());
		if(hotelOrder.getStatus().intValue() != 6){
			hotelOrder.setStatus(6);
			if(StringUtils.isEmpty(hotelOrder.getRoomIdList())){
                hotelOrder.setRoomIdList("-1,");
            }
			try {
				hotelOrderService.saveOrderAndDayStatus(hotelOrder);
			}catch (Exception e){
				e.printStackTrace();
				return R.error(e.getMessage());
			}
		}else{
			hotelOrderService.save(hotelOrder);
		}
		return R.ok("结算成功");
	}


	/**
	 * http://127.0.0.1:8090/hotel/hotelOrder/inRoom?memberName=hjb&memberMobile=15230060935&roomId=9&dayNum=3&payMoney=1000&outTimeStr=2019-10-20 16:00:00&sso_token=57697d62-e0a6-4702-85cb-94dd8f90c13e
	 * 入住
	 */
	@ResponseBody
	@RequestMapping("/inRoom")
	@RequiresPermissions("hotel:hotelOrder:edit")
	public R inRoom( HotelOrder hotelOrder,@RequestParam("outTimeStr") String outTimeStr) throws ParseException {

		Object r =  RequestParCheckUtils.checkPar(hotelOrder, new String[]{
				"memberName",
				"memberMobile",
				"roomId",
				"orderType"
		});

		if(!r.equals(true)){
			return R.error(r.toString());
		}
		Date outDate =  DateUtils.pasre(outTimeStr, DateUtils.DATE_TIME_PATTERN);


		HotelRoom hotelRoom = hotelRoomService.get(hotelOrder.getRoomId());
		hotelOrder.setRoomName(hotelRoom.getName());
		hotelOrder.setRoomTypeId(hotelRoom.getTypeId());
		hotelOrder.setRoomTypeName(hotelRoom.getTypeName());
		hotelOrder.setRoomIdList(hotelRoom.getId()+",");

		if(hotelOrder.getOrderType().intValue() == 1){
			hotelOrder.setMoneyCodeName("钟点房");
		}else{
			HotelRoomMoneyCode hotelRoomMoneyCode =  hotelRoomMoneyCodeService.get(hotelOrder.getMoneyCodeId());
			hotelOrder.setMoneyCodeName(hotelRoomMoneyCode.getName());
		}
		AtomicReference<Double> totalMoney = new AtomicReference<>(0d);
		if(null == hotelOrder.getId()){
			//订单不存在的情况下才设置 开始时间  1是钟点房
			if(hotelOrder.getOrderType().intValue() == 1){
				hotelOrder.setInDate(DateUtils.pasre(DateUtils.format(new Date()),DateUtils.DATE_PATTERN));
				hotelOrder.setInTime(new Date());
				Calendar calendar = Calendar.getInstance();
				calendar.add(Calendar.HOUR_OF_DAY,hotelOrder.getInHour());
				outDate = calendar.getTime();
			}else{
				hotelOrder.setInDate(DateUtils.pasre(DateUtils.format(new Date()),DateUtils.DATE_PATTERN));
				//hotelOrder.setInTime(DateUtils.getCustomTime(14));
				hotelOrder.setInTime(new Date());
			}
		}
		hotelOrder.setOutDate(DateUtils.pasre(DateUtils.format(outDate),DateUtils.DATE_PATTERN));
		hotelOrder.setOutTime(outDate);

		if(hotelOrder.getInTime().after(hotelOrder.getOutTime())){
			return R.error("退房时间不能小于当前时间");
		}
		LvyouScenicSpot lvyouScenicSpot  =  lvyouScenicSpotService.get(hotelRoom.getHotelId());
		hotelOrder.setHotelMobile(lvyouScenicSpot.getHotelMobile());
		List<Date> dateList =  DateUtils
				.getStartTimeAndEndTimeEveryDay(
						hotelOrder.getInDate(),
						hotelOrder.getOutDate());
		List<String> noStockList = new LinkedList<>();

		if(hotelOrder.getId() == null){
			dateList.forEach(item ->{
				String itemDay = DateUtils.format(item);
				HotelRoomDayStatus hotelRoomDayStatus =  hotelRoomDayStatusService.getByRoomIdAndDay(hotelOrder.getRoomId(),itemDay);
				if(null != hotelRoomDayStatus){
					switch (hotelRoomDayStatus.getStatus().intValue()){
						case 3:
							noStockList.add(itemDay+"在住");
							break;
						case 5:
							noStockList.add(itemDay+"被预订");
							break;
					}
				}
			});
			if(noStockList.size() > 0){
				return R.error(Arrays.toString(noStockList.toArray()));
			}
		}
		List<String> everyDayMoeny = new ArrayList<>();
		if(hotelOrder.getOrderType().intValue() == 1){
			HotelRoomType hotelRoomType = 	hotelRoomTypeService.get(hotelOrder.getRoomTypeId());
			if(null == hotelRoomType.getMoney() || hotelRoomType.getMoney() <= 0){
				return R.error("请先设置小时房价");
			}
			everyDayMoeny.add(hotelRoomType.getMoney().toString());
			totalMoney.set(hotelRoomType.getMoney()*hotelOrder.getInHour());
		}else{
			//设置每日价格
			/*dateList.forEach(item->{
				String itemDay = DateUtils.format(item);
				HotelEveryDayMoney hotelEveryDayMoney =  hotelEveryDayMoneyService.getByCodeIdAndDate(hotelOrder.getMoneyCodeId(), itemDay);
				if(null == hotelEveryDayMoney){
					noStockList.add(itemDay+"没有设置房价");
					return ;
				}
				everyDayMoeny.add(hotelEveryDayMoney.getMoney().toString());
				totalMoney.updateAndGet(v -> new Double((double) (v + hotelEveryDayMoney.getMoney())));
			});*/
			for(int d = 0 ; d < dateList.size(); d++){
				Date item = dateList.get(d);
				String itemDay = DateUtils.format(item);
				HotelEveryDayMoney hotelEveryDayMoney =  hotelEveryDayMoneyService.getByCodeIdAndDate(hotelOrder.getMoneyCodeId(), itemDay);
				if(null == hotelEveryDayMoney){
					noStockList.add(itemDay+"没有设置房价");
					break;
				}

				if(d < (dateList.size() -1)){
					everyDayMoeny.add(hotelEveryDayMoney.getMoney().toString());
					totalMoney.updateAndGet(v -> new Double((double) (v + hotelEveryDayMoney.getMoney())));
				}
			}
			if(noStockList.size() > 0){
				return R.error(Arrays.toString(noStockList.toArray()));
			}
		}

		if(totalMoney.get() <= 0){
			return R.error("房价为0 请检查每日房价配置");
		}
		hotelOrder.setRoomNumber(1);
		hotelOrder.setRoomTotalNum(1);
		hotelOrder.setMemberLevel("WX");
		hotelOrder.setDayNum(DateUtils.caculateTotalTime(DateUtils.format(hotelOrder.getInDate()),DateUtils.format(hotelOrder.getOutDate()))-1);
		hotelOrder.setFloorId(hotelRoom.getFloorId());
		hotelOrder.setFloorName(hotelRoom.getFloorName());
		hotelOrder.setEveryMoney(Arrays.toString(everyDayMoeny.toArray()));
		hotelOrder.setPayMoney(0d);
		hotelOrder.setTotalMoney(totalMoney.get() *  hotelOrder.getRoomNumber());
		hotelOrder.setUserId(hotelRoom.getUserId());
		hotelOrder.setUserName(hotelRoom.getUserName());
		hotelOrder.setHotelId(hotelRoom.getHotelId());
		hotelOrder.setHotelName(hotelRoom.getHotelName());
		//入住
		hotelOrder.setStatus(1);
		hotelOrder.setOrderNo(SnowflakeIdWorker.getOrderNo("H"));
		try {
			int z =  hotelOrderService.saveOrderAndDayStatus(hotelOrder);
		}catch (Exception e){
			e.printStackTrace();
			return R.error(e.getMessage());
		}
		return R.ok().put("i",hotelOrder);
	}


	/**
	 * http://127.0.0.1:8090/hotel/hotelOrder/inRoom?memberName=hjb&memberMobile=15230060935&roomId=9&dayNum=3&payMoney=1000&outTimeStr=2019-10-20 16:00:00&sso_token=57697d62-e0a6-4702-85cb-94dd8f90c13e
	 * 入住多人
	 */
	@ResponseBody
	@RequestMapping("/inRoomDuo")
	@RequiresPermissions("hotel:hotelOrder:edit")
	public R inRoomDuo( HotelOrder hotelOrder,@RequestParam("outTimeStr") String outTimeStr) throws ParseException {

		Object r =  RequestParCheckUtils.checkPar(hotelOrder, new String[]{
				"memberName",
				"memberMobile",
				"orderType"
		});

		if(!r.equals(true)){
			return R.error(r.toString());
		}
		Date outDate =  DateUtils.pasre(outTimeStr, DateUtils.DATE_TIME_PATTERN);


		HotelRoomType hotelRoomType = hotelRoomTypeService.get(hotelOrder.getRoomTypeId());

		hotelOrder.setRoomTypeId(hotelRoomType.getId());
		hotelOrder.setRoomTypeName(hotelRoomType.getName());

		LvyouScenicSpot lvyouScenicSpot  =  lvyouScenicSpotService.get(hotelRoomType.getHotelId());
		hotelOrder.setHotelMobile(lvyouScenicSpot.getHotelMobile());

		if(hotelOrder.getOrderType().intValue() == 1){
			hotelOrder.setMoneyCodeName("钟点房");
		}else{
			HotelRoomMoneyCode hotelRoomMoneyCode =  hotelRoomMoneyCodeService.get(hotelOrder.getMoneyCodeId());
			hotelOrder.setMoneyCodeName(hotelRoomMoneyCode.getName());
		}
		AtomicReference<Double> totalMoney = new AtomicReference<>(0d);
		if(null == hotelOrder.getId()){
			//订单不存在的情况下才设置 开始时间  1是钟点房
			if(hotelOrder.getOrderType().intValue() == 1){
				hotelOrder.setInDate(DateUtils.pasre(DateUtils.format(new Date()),DateUtils.DATE_PATTERN));
				hotelOrder.setInTime(new Date());
				Calendar calendar = Calendar.getInstance();
				calendar.add(Calendar.HOUR_OF_DAY,hotelOrder.getInHour());
				outDate = calendar.getTime();
			}else{
				hotelOrder.setInDate(DateUtils.pasre(DateUtils.format(new Date()),DateUtils.DATE_PATTERN));
				//hotelOrder.setInTime(DateUtils.getCustomTime(14));
				hotelOrder.setInTime(new Date());
			}
		}
		hotelOrder.setOutDate(DateUtils.pasre(DateUtils.format(outDate),DateUtils.DATE_PATTERN));
		hotelOrder.setOutTime(outDate);

		if(hotelOrder.getInTime().after(hotelOrder.getOutTime())){
			return R.error("退房时间不能小于当前时间");
		}


		List<Date> dateList =  DateUtils
				.getStartTimeAndEndTimeEveryDay(
						hotelOrder.getInDate(),
						hotelOrder.getOutDate());
		List<String> noStockList = new LinkedList<>();

		if(hotelOrder.getId() == null){
			List<HotelRoom> hotelRooms = new ArrayList<>();
			String [] hotelArray =  hotelOrder.getRoomIdList().split(",");
			for(String hid : hotelArray){
					HotelRoom hotelRoom = hotelRoomService.get(Long.parseLong(hid));
					hotelRooms.add(hotelRoom);
			}
			if(hotelRooms.size() == 0){
				return R.error("请选择一个房间");
			}
			hotelOrder.setRoomId(hotelRooms.get(hotelRooms.size()-1).getId());
			hotelOrder.setRoomName(getRoomNameList(hotelRooms));
			hotelOrder.setRoomIdList(getRoomIdList(hotelRooms));
			hotelOrder.setRoomNumber(hotelRooms.size());
			hotelOrder.setRoomTotalNum(hotelRooms.size());
			hotelOrder.setHotelRooms(hotelRooms);
			for(HotelRoom room : hotelRooms){
				dateList.forEach(item ->{
					String itemDay = DateUtils.format(item);
					HotelRoomDayStatus hotelRoomDayStatus =  hotelRoomDayStatusService.getByRoomIdAndDay(room.getId(),itemDay);
					if(null != hotelRoomDayStatus){
						switch (hotelRoomDayStatus.getStatus().intValue()){
							case 3:
								noStockList.add(itemDay+"在住");
								break;
							case 5:
								noStockList.add(itemDay+"被预订");
								break;
						}
					}
				});
			}
			if(noStockList.size() > 0){
				return R.error(Arrays.toString(noStockList.toArray()));
			}
		}
		List<String> everyDayMoeny = new ArrayList<>();
		if(hotelOrder.getOrderType().intValue() == 1){
			if(null == hotelRoomType.getMoney() || hotelRoomType.getMoney() <= 0){
				return R.error("请先设置小时房价");
			}
			everyDayMoeny.add(hotelRoomType.getMoney().toString());
			totalMoney.set(hotelRoomType.getMoney()*hotelOrder.getInHour());
		}else{
			for(int d = 0 ; d < dateList.size(); d++){
				Date item = dateList.get(d);
				String itemDay = DateUtils.format(item);
				HotelEveryDayMoney hotelEveryDayMoney =  hotelEveryDayMoneyService.getByCodeIdAndDate(hotelOrder.getMoneyCodeId(), itemDay);
				if(null == hotelEveryDayMoney){
					noStockList.add(itemDay+"没有设置房价");
					break;
				}
				if(d < (dateList.size() -1)){
					everyDayMoeny.add(hotelEveryDayMoney.getMoney().toString());
					totalMoney.updateAndGet(v -> new Double((double) (v + hotelEveryDayMoney.getMoney())));
				}
			}
			if(noStockList.size() > 0){
				return R.error(Arrays.toString(noStockList.toArray()));
			}
		}

		if(totalMoney.get() <= 0){
			return R.error("房价为0 请检查每日房价配置");
		}

		hotelOrder.setMemberLevel("WX");
		hotelOrder.setDayNum(DateUtils.caculateTotalTime(DateUtils.format(hotelOrder.getInDate()),DateUtils.format(hotelOrder.getOutDate()))-1);
		hotelOrder.setEveryMoney(Arrays.toString(everyDayMoeny.toArray()));
		hotelOrder.setPayMoney(0d);
		hotelOrder.setTotalMoney(totalMoney.get() *  hotelOrder.getRoomNumber());
		hotelOrder.setUserId(hotelRoomType.getUserId());
		hotelOrder.setUserName(hotelRoomType.getUserName());
		hotelOrder.setHotelId(hotelRoomType.getHotelId());
		hotelOrder.setHotelName(hotelRoomType.getHotelName());
		//入住
		hotelOrder.setStatus(1);
		hotelOrder.setOrderNo(SnowflakeIdWorker.getOrderNo("H"));
		try {
			int z =  hotelOrderService.saveOrderAndDayStatus(hotelOrder);
		}catch (Exception e){
			e.printStackTrace();
			return R.error(e.getMessage());
		}
		return R.ok();
	}


	@RequestMapping("/ruzhuStatus")
	@ResponseBody
	public R ruzhuStatus(@RequestParam("id") Long id){
		HotelOrder hotelOrder  =  hotelOrderService.get(id);
		if(hotelOrder.getRoomTotalNum() == 1){
			String[] dd = hotelOrder.getRoomIdList().split(",");
			hotelOrder.setRoomId(Long.parseLong(dd[0]));
		}
		hotelOrder.setStatus(1);
		try {
			int z =  hotelOrderService.saveOrderAndDayStatus(hotelOrder);
		}catch (Exception e){
			e.printStackTrace();
			return R.error(e.getMessage());
		}
		return R.ok();
	}



	@RequestMapping("/exportExcel")
	@ResponseBody
	public void exportExcel(HttpServletRequest request,HttpServletResponse response) throws Exception {
		 String sso_token =  request.getParameter("sso_token");
		 if(StringUtils.isEmpty(sso_token)){
		 	return;
		 }else{
			 Map query = (Map) redisUtil.get(sso_token+"query");
			 query.remove("offset");
			 query.remove("limit");
			 List<HotelOrder>  hotelOrders =  hotelOrderService.list(query);
			 new DachuExcelUtils<HotelOrder>().exportExcle("酒店订单统计-" + DateUtils.format(new Date()), HotelOrder.class, hotelOrders, response);
		 }

	}

	/*List<ShopOrder> orderList = shopOrderService.list(params);

	*/


	String getRoomNameList(List<HotelRoom> hotelRooms){
		StringBuilder stringBuilder = new StringBuilder();
		for(HotelRoom item : hotelRooms){
			stringBuilder.append(item.getName()).append(",");
		}
		return stringBuilder.toString();
	}
	String getRoomIdList(List<HotelRoom> hotelRooms){
		StringBuilder stringBuilder = new StringBuilder();
		for(HotelRoom item : hotelRooms){
			stringBuilder.append(item.getId()).append(",");
		}
		return stringBuilder.toString();
	}

	/**
	 * /hotel/hotelOrder/getOrderByRoomId?roomId=9
	 * 入住
	 */
	@ResponseBody
	@RequestMapping("/getOrderByRoomId")
	@RequiresPermissions("hotel:hotelOrder:edit")
	public R getOrderByRoomId(@RequestParam("roomId") Long roomId,
							  @RequestParam("status") Integer sta,
							  @RequestParam(value = "totalNum",required = false,defaultValue = "1") Integer totalNum
							  )  {
		List<Integer> status = new ArrayList<>();
		status.add(0);
		status.add(1);
		status.add(3);
		status.add(4);

	  Map query  = 	MapBuild.getInstand()
				.setKV("statusList", status);
		query.put("roomIdList", roomId+",");
	  /*	//说明是预定状态
		if(sta.intValue() == 5 || totalNum > 1){
			query.put("roomIdList", roomId+",");
		}else{
			query.put("roomId", roomId);
		}*/
		List<HotelOrder> hotelOrders =  hotelOrderService.
				list(query);
		HotelOrder hotelOrder = new HotelOrder();
		if(null != hotelOrders && hotelOrders.size() > 0){
			hotelOrder = hotelOrders.get(0);
		}
		return R.ok().put("i", hotelOrder);
	}


	@RequestMapping("updateRoom")
	@ResponseBody
	public R updateRoom(@RequestParam("id")Long id ,@RequestParam("name") String name){
			HotelRoom thisRoom =  hotelRoomService.get(id);
			HotelRoom tempRoom = new HotelRoom();
		tempRoom.setId(thisRoom.getId());
		tempRoom.setName(thisRoom.getName());
		tempRoom.setTypeId(thisRoom.getTypeId());
		tempRoom.setTypeName(thisRoom.getTypeName());
		tempRoom.setFloorId(thisRoom.getFloorId());
		tempRoom.setFloorName(thisRoom.getFloorName());
		tempRoom.setHotelId(thisRoom.getHotelId());
		tempRoom.setHotelName(thisRoom.getHotelName());
		tempRoom.setPic(thisRoom.getPic());
		tempRoom.setPx(thisRoom.getPx());
		    HotelRoom newRoom =  hotelRoomService.getHotelIdAndName(tempRoom.getHotelId(),name);

		    if(null == newRoom){
		    	return R.ok("房间不存在");
			}

		    if(!newRoom.getTypeId().equals(thisRoom.getTypeId())){
		    	return R.ok("不同房型之间不能互换");
			}

		    thisRoom.setName(newRoom.getName());
		    thisRoom.setTypeId(newRoom.getTypeId());
		    thisRoom.setTypeName(newRoom.getTypeName());
		    thisRoom.setFloorId(newRoom.getFloorId());
		    thisRoom.setFloorName(newRoom.getFloorName());
		    thisRoom.setPic(newRoom.getPic());
		    thisRoom.setPx(newRoom.getPx());
		thisRoom.setHotelId(newRoom.getHotelId());
		thisRoom.setHotelName(newRoom.getHotelName());



			newRoom.setName(tempRoom.getName());
			newRoom.setTypeId(tempRoom.getTypeId());
			newRoom.setTypeName(tempRoom.getTypeName());
			newRoom.setFloorId(tempRoom.getFloorId());
			newRoom.setFloorName(tempRoom.getFloorName());
			newRoom.setPic(tempRoom.getPic());
			newRoom.setPx(tempRoom.getPx());
		newRoom.setHotelId(tempRoom.getHotelId());
		newRoom.setHotelName(tempRoom.getHotelName());
		hotelRoomService.save(newRoom);
		hotelRoomService.save(thisRoom);
		return R.ok();
	}
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("hotel:hotelOrder:edit")
	public R remove( Long id){
		if(hotelOrderService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("hotel:hotelOrder:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		hotelOrderService.batchRemove(ids);
		return R.ok();
	}
	
}